<?php
/* 
	This file is Copyright Matthew Wilkinson 2009.
	
	This file is part of Oxygen.

	Oxygen is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License version 3 as published by
	the Free Software Foundation at http://www.gnu.org/licenses/gpl-3.0.html

	Oxygen is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

*/

function __autoload($class)
	{
	$class = str_split($class);
	$newstring = '';
	foreach ($class as $char)
		{
		$ord = ord($char);
		// ASCII ordinal must be the same as that for an underscore, dash, number or letter (upper or lowercase)
		if ($ord == 45 OR $ord == 95 OR ($ord >= 48 AND $ord <= 57) OR ($ord >= 65 AND $ord <= 90) OR ($ord >= 97 AND $ord <= 122))
			{
			$newstring .= $char;
			}
		elseif ($ord == 92)
			{
			// Eliminate namespaces
			$newstring = '';
			}
		}
	$class = $newstring;
	// $class is now definately alphanumeric and is safe to check against the filesystem
	
	// Check core classes
	if (defined('_OXYGEN_CORE_DIR'))
		{
		$dirname = _OXYGEN_CORE_DIR;
		}
	else
		{
		$dirname = './core';
		}
	$dir = scandir($dirname);
	foreach ($dir as $file)
		{
		if (stripos($file, $class) === 0)
			{
			require_once($dirname . '/' . $class . '.php');
			return TRUE;
			}
		}
	
	// Check components
	if (defined('_OXYGEN_COMPONENTS_DIR'))
		{
		$dirname = _OXYGEN_COMPONENTS_DIR;
		}
	else
		{
		$dirname = './components';
		}
	$dir = scandir($dirname);
	foreach ($dir as $file)
		{
		if (strpos($file, $class) === 0)
			{
			require_once($dirname . '/' . $class . '/' . $class . '.php');
			return TRUE;
			}
		}
	
	if (defined('_OXYGEN_DRIVERS_DIR'))
		{
		$dirname = _OXYGEN_DRIVERS_DIR;
		}
	else
		{
		$dirname = './drivers';
		}
	$dir = scandir($dirname);
	foreach ($dir as $d)
		{
		if (strpos($d, '.') === FALSE)
			{
			$dir2 = scandir($dirname . '/' . $d);
			foreach ($dir2 as $file)
				{
				if (stripos($file, $class) === 0)
					{
					require_once($dirname . '/' . $d . '/' . $class . '.php');
					return TRUE;
					}
				}
			}
		}
	}

?>